package com.suixun.util.enums;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum PlatformEnum {
	/*
	 * 电商平台
	 */
	TAOBAO("taobao"), TMALL("tmall"), OTHER("other");

	private static final Pattern taobaoPattern = Pattern
			.compile("item.taobao.com/item.htm.*(?=[\\?&]id=(\\d+))");

	private static final Pattern tmallPattern = Pattern
			.compile("detail.tmall.com/item.htm.*(?=[\\?&]id=(\\d+))");

	private String value;

	PlatformEnum(String value) {
		this.value = value;
	}

	public String value() {
		return value;
	}

	public static String getShopUrl(String platform, String sellProductId) {
		if (TAOBAO.value().equals(platform)) {
			return "http://item.taobao.com/item.htm?id=" + sellProductId;
		} else if (TMALL.value().equals(platform)) {
			return "http://detail.tmall.com/item.htm?id=" + sellProductId;
		} else {
			return "";
		}
	}

	public static Long parseItemId(String detailUrl) {
		Matcher taobaoMatcher = taobaoPattern.matcher(detailUrl);
		try {
			if (taobaoMatcher.find())
				return Long.parseLong(taobaoMatcher.group(1));
		} catch (Exception e) {
			e.printStackTrace();
		}
		Matcher tmallMatcher = tmallPattern.matcher(detailUrl);
		try {
			if (tmallMatcher.find())
				return Long.parseLong(tmallMatcher.group(1));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		System.out.println(parseItemId("http://www.taobao.com/go/act/sale/xmtbdurex.php?spm=1.6659421.915625793.1.CFbtgh&ad_id=&am_id=13010693466e7a465d29&cm_id=&pm_id="));

		System.out.println(parseItemId("http://detail.tmall.com/item.htm?spm=0.0.0.0.r7RXEc&id=18461620915&scene=taobao_shop"));

		System.out.println(parseItemId("http://detail.tmall.com/item.htm?spm=a220o.1000855.0.0.3BRqxg&id=15495461928&pos=3&uuid=2767b1f8-4ec4-4d9f-af16-9194f831f9ea&scm=1003.3.03054.1_AB-LV32_181-PR32&acm=03054.1003.1.474.15495461928_1&rn="));

		System.out.println(parseItemId("http://item.taobao.com/item.htm?id=18684909581&_fb=MTBxZmQyfDExaWEwfDJhfDcwMjAxNzE0NTN8Zmx2fDk0MDQ3Ng%3D%3D&spm=1.6659421.882450129.24.CFbtgh"));

		System.out.println(parseItemId("http://item.taobao.com/item.htm?spm=2013.1.0.0.8OJaLY&id=21138775331"));

	}
}
